package ckw.gui.wizards;

import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;

import org.eclipse.jface.wizard.WizardPage;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.DateTime;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;

public class WzdElectionPg1 extends WizardPage {
	private Text txtName;
	private DateTime dateStartTime;
	private DateTime timeStartTime;
	private DateTime dateEndTime;
	private DateTime timeEndTime;
	
	/**
	 * Create the wizard.
	 */
	public WzdElectionPg1() {
		super("wizardPage");
		setTitle("Dodaj wybory");
		setDescription("Krok 1. Podaj podstawowe dane wyborow");
	}

	/**
	 * Create contents of the wizard.
	 * @param parent
	 */
	public void createControl(Composite parent) {
		Composite container = new Composite(parent, SWT.NULL);

		setControl(container);
		container.setLayout(new GridLayout(3, false));
		
		Label lblNazwa = new Label(container, SWT.NONE);
		lblNazwa.setText("Nazwa:");
		
		txtName = new Text(container, SWT.BORDER);
		txtName.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 2, 1));
		
		Label lblDataRozpoczecia = new Label(container, SWT.NONE);
		lblDataRozpoczecia.setText("Data rozpoczecia:");
		
		dateStartTime = new DateTime(container, SWT.BORDER | SWT.DROP_DOWN);
		
		timeStartTime = new DateTime(container, SWT.BORDER | SWT.TIME | SWT.SHORT);
		
		Label lblDataZakonczenia = new Label(container, SWT.NONE);
		lblDataZakonczenia.setText("Data zakonczenia:");
		
		dateEndTime = new DateTime(container, SWT.BORDER | SWT.DROP_DOWN);
		
		timeEndTime = new DateTime(container, SWT.BORDER | SWT.TIME | SWT.SHORT);
		
		new Label(container, SWT.NONE);
	}

	private Timestamp dateTimeToTimestamp(DateTime date, DateTime time) {
		DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
		java.util.Date utilDate;
		try {
			utilDate = dateFormat.parse(date.getYear() + "-" + date.getMonth() + "-" + date.getDay() + " " + time.getHours() + ":" + time.getMinutes());
			return new Timestamp(utilDate.getTime());
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}
	
	public String getElectionName() {
		return txtName.getText();
	}
	public Timestamp getElectionStartTime() {
		return dateTimeToTimestamp(dateStartTime, timeStartTime);
	}
	public Timestamp getElectionEndTime() {
		return dateTimeToTimestamp(dateEndTime, timeEndTime);
	}
	
	public boolean setPageComplete() {
		return false;
	}
}
